package com.example.springboot.controller;

import com.example.springboot.entity.Book;
import com.example.springboot.entity.Result;
import com.example.springboot.service.impl.BookServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookServiceImpl bookServiceImpl;

    // 增加书本接口
    @PostMapping("/add")
    Result add(@RequestBody Book book){
        bookServiceImpl.add(book);

        return Result.success("添加书籍成功");
    }

    // 删除书本接口
    @DeleteMapping("/delete")
    Result delete(@RequestParam Integer id){
        bookServiceImpl.removeById(id);

        return Result.success("删除书籍成功");
    }

    // 修改书本接口
    @PutMapping("/update")
    Result update(@RequestBody Book book, @RequestParam Integer id){
        book.setId(id);
        bookServiceImpl.updateById(book);

        return Result.success("修改书籍成功");
    }

    // 查找书本接口
    @GetMapping("/get")
    Result get(@RequestParam Integer id){
        Book book = bookServiceImpl.getById(id);

        return Result.success(book);
    }

}
